//
//  ListViewController.m
//  AlgorithmDemo
//
//  Created by zxs on 2020/8/5.
//  Copyright © 2020 zxs. All rights reserved.
//

#import "ListViewController.h"
#import "List.h"

@interface ListViewController ()
// 输入的内容
@property (weak, nonatomic) IBOutlet UITextView *inputTextView;

// 链表正向内容
@property (weak, nonatomic) IBOutlet UILabel *listLabel;

// 链表反向内容
@property (weak, nonatomic) IBOutlet UILabel *reverseLabel;

// 链表
@property (strong, nonatomic) List *list;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.list = [[List alloc] init];
}

// 生成链表
- (IBAction)generateButtonTouched:(id)sender {
    NSString *input = self.inputTextView.text;
    NSArray *array = [input componentsSeparatedByString:@","];
    [self.list addNodes:array];
    
    NSArray *nodes = [self.list readAllNodes];
    NSMutableArray *messages = [NSMutableArray array];
    [nodes enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ListNode *node = (ListNode *)obj;
        NSString *message = [NSString stringWithFormat:@"%@", node.data];
        [messages addObject:message];
    }];
    self.listLabel.text = [messages componentsJoinedByString:@";"];
}

// 反向链表
- (IBAction)reverseButtonTouched:(id)sender {
    [self.list reverse];
    
    NSArray *nodes = [self.list readAllNodes];
    NSMutableArray *messages = [NSMutableArray array];
    [nodes enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ListNode *node = (ListNode *)obj;
        NSString *message = [NSString stringWithFormat:@"%@", node.data];
        [messages addObject:message];
    }];
    self.reverseLabel.text = [messages componentsJoinedByString:@";"];
}

@end
